package com.pp.buvaka.table;

import java.util.Arrays;
import java.util.Iterator;

public class Row implements Iterable<Field> {
    public static final int MAX_SIZE = 26;

    private Field[] fields; //Row - is an array of fields

    public int getSize() {
        return fields.length;
    }

    public Row (int size){
        assert (size > 0 && size <= MAX_SIZE);

        fields = new Field[size];
    }

    public Field get(int index){
        assert(index < fields.length && index >=0);

        return fields[index];
    }

    public void set(Field value, int index){
        assert(index < fields.length && index >=0);

        fields[index] = value;
    }

    public Field get (String index){
        assert (index.length() == 1 && index.compareTo("Z") <=0 && index.compareTo("A") >= 0);

        return get(Alphabet.valueOf(index).getIndex());
    }

    /**
    *Converts simple array of string into the Row
    *@param strings - initial array of string
    **/
    public void setFieldsFromStringArray(String[] strings){
        assert (strings.length == this.getSize());

        for (int i = 0; i < strings.length; i++) {
            this.set(new Field(strings[i]), i);
        }
    }

    @Override
    public String toString(){
        String ret = "";
        for (Field f : fields){
            ret = ret + f.toString() + ":";
        }
        return ret;
    }

    /**
     * Returns an iterator over a set of elements of type T.
     *
     * @return an Iterator.
     */
    @Override
    public Iterator<Field> iterator() {
        return Arrays.asList(fields).iterator();
    }
}
